package com.oozol.lieluyun.controller;

import com.oozol.lieluyun.config.GlobalService;
import com.oozol.lieluyun.entity.ResultData;
import com.oozol.lieluyun.entity.UserEntity;
import com.oozol.lieluyun.service.IJenkinsService;
import java.util.Objects;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SystemController {
   @Autowired
   private IJenkinsService jenkinsServices;

   @PostMapping({"/api/login"})
   public ResultData<UserEntity> login(HttpServletRequest request, UserEntity userEntity) throws Exception {
      ResultData res = new ResultData();
      String userName = this.jenkinsServices.jenkinsLogin(userEntity);
      if (!Objects.isNull(userName) && Objects.equals(userEntity.getUsername(), userName)) {
         request.getSession().setAttribute("loginName", userEntity.getUsername());
         GlobalService.userEntity = userEntity;
         res.setMsg("操作成功");
         res.setData(userEntity);
         return res;
      } else {
         res.setMsg("账号密码不匹配");
         res.setCode(400);
         return res;
      }
   }

   @GetMapping({"/api/loginOut"})
   public ResultData<String> loginOut(HttpServletRequest request) {
      request.getSession().invalidate();
      ResultData res = new ResultData();
      res.setMsg("操作成功");
      res.setData("/login");
      return res;
   }
}
